<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
        函数的柯里化：通过函数调用继续返回函数的方式，实现多次接收参数，最后统一处理的编码形式
      
      */
      //封装函数1
      function add1(a, b, c) {
        return a + b + c;
      }

      add1(1, 2);

      //柯里化封装
      function add2(a) {
        return function (b) {
          return function (c) {
            return a + b + c;
          };
        };
      }

      add2(1)(2)(3);

      //封装一个拼接路径的函数
      function addUrl1(protocol, host, port) {
        return `${protocol}://${host}:${port}`;
      }
      addUrl1("http", "127.0.0.1", "8888");
      addUrl1("http", "127.0.0.1", "6666");
      addUrl1("http", "localhost", "3000");
      addUrl1("http", "localhost", "3300");

      //把上边函数改为函数柯里化写法
      function addUrl2(protocol) {
        return function (host) {
          return function (port) {
            return `${protocol}://${host}:${port}`;
          };
        };
      }

      const proUrl2 = addUrl2("http");
      console.log(proUrl2("127.0.0.1")(8888));
      console.log(proUrl2("127.0.0.1")(6666));
      console.log(proUrl2("127.0.0.1")(5555));

      const proHostUrl2 = proUrl2("localhost");
      console.log(proHostUrl2(1234));
      console.log(proHostUrl2(12345));
    </script>
  </body>
</html>
